package com.chenyi.web.controller.shop;

import com.chenyi.common.annotation.UserRequired;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.entity.SysAdminMenu;
import com.chenyi.common.core.domain.model.LoginAdmin;
import com.chenyi.common.core.domain.model.LoginShop;
import com.chenyi.common.utils.ServletUtils;
import com.chenyi.framework.web.service.ShopTokenService;
import com.chenyi.common.core.domain.entity.SysShopMenu;
import com.chenyi.shop.service.ISysShopMenuService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 菜单权限Controller
 *
 * @author chenyi
 * @date 2020-10-24
 */
@RestController
@RequestMapping("/shop/menu")
public class ShopMenuController extends BaseController {

    @Autowired
    private ISysShopMenuService menuService;

    @Autowired
    private ShopTokenService tokenService;


    /**
     * 获取菜单下拉树列表
     */
    @UserRequired
    @GetMapping("/treeselect")
    public ResponseEntity<Object> treeselect(SysShopMenu menu)
    {
        LoginShop loginShop = tokenService.getLoginShop(ServletUtils.getRequest());
        Long shopId = loginShop.getShop().getShopId();
        List<SysShopMenu> menus = menuService.selectShopMenuList(menu, shopId);
        return ResponseEntity.ok(menuService.buildShopMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @UserRequired
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public ResponseEntity<Object> roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        LoginShop loginShop = tokenService.getLoginShop(ServletUtils.getRequest());
        List<SysShopMenu> menus = menuService.selectShopMenuList(loginShop.getShop().getShopId());
        Map<String,Object> data = Maps.newHashMap();
        data.put("checkedKeys", menuService.selectShopMenuListByRoleId(roleId));
        data.put("menus", menuService.buildShopMenuTreeSelect(menus));
        return ResponseEntity.ok(data);
    }
}
